package com.edu.test.reflect.edu04;

import com.sun.javafx.collections.MappingChange;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @作者 five-five
 * @创建时间 2020/12/25
 */
public class ExcelUtilByPOI {
    private final Integer LOCK_NUMBER=1;
    //储存容器
    private volatile List<Map<String, Object>> mapList = new ArrayList<>();
    //输入流
    private FileInputStream ins = null;
    //excel读取对象
    private XSSFWorkbook xssfWorkbook;
    //excel的sheet对象
    private XSSFSheet sheet;
    //excel的FirstRow对象
    public XSSFRow firstRow;

    private void initilize(String path,String sheetName) {
        try {
            ins = new FileInputStream(path);
            xssfWorkbook = new XSSFWorkbook(ins);
            sheet = xssfWorkbook.getSheet(sheetName);
            firstRow = sheet.getRow(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private int threadNumber;
    //开始执行读取方法
    public List<Map<String,Object>> readExcelDateToListMap(String path, String sheetName,int threadNumber){
        this.threadNumber = threadNumber;
        initilize(path,sheetName);
        for (int i = 0; i < threadNumber; i++) {
            MyThread myThread = new MyThread(i);
            myThread.setName("线程"+(i+1));
            System.out.println("线程-"+myThread.getName()+"\t线程的起始值"+myThread.start);
            myThread.run();
        }
        return mapList;
    }


    class MyThread extends Thread {
        private volatile int start;//起始行数
        private volatile int currindex;//当前线程数
        private int worksize=sheet.getLastRowNum()/threadNumber==0
                ?sheet.getLastRowNum()/threadNumber
                :sheet.getLastRowNum()/threadNumber+1;//读取行数

        public MyThread(int currindex){
            this.currindex = currindex;
            this.start=currindex*worksize+1;
        }

        @Override
        public void run() {
            readExcel(start);
        }

        /**
         * @param start
         * @return
         */
        public void readExcel(int start) {
            if (sheet == null) {
                //sheet为空，开始返回
                return;
            }
            if (sheet != null) {
                int lastRowNum = sheet.getLastRowNum();//最后一行的值
                //判断是不是超出了自己的工作范围
                int end=start+worksize>lastRowNum?lastRowNum:start+worksize;
                //根据开始行数和终止行数进行循环,上一把锁实现原子化
                synchronized (LOCK_NUMBER) {
                    for (int i = start; i < end; i++) {
                        Map<String, Object> map = new LinkedHashMap<>();
                        XSSFRow row = sheet.getRow(i);
                        //开始循环cell
                        for (int j = 0; j < row.getLastCellNum(); j++) {
                            XSSFCell cell = row.getCell(j);
                            //列为空，跳出当前循环
                            if (cell == null) {
                                continue;
                            }
                            //开始取值
                            String value = getValue(cell);
                            map.put(firstRow.getCell(j).toString(), cell);
                        }
                        mapList.add(map);
                    }
                }
            }
        }


        private String getValue(XSSFCell xssfCell) {
            if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
                return String.valueOf(xssfCell.getBooleanCellValue());
            } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
                return String.valueOf(xssfCell.getNumericCellValue());
            } else {
                return String.valueOf(xssfCell.getStringCellValue());
            }
        }
    }
}
